package com.fusheng.demo.accessibilityservice

import android.accessibilityservice.AccessibilityService
import android.content.Intent
import android.util.Log
import android.view.accessibility.AccessibilityEvent

class AutoShareWXService : AccessibilityService() {


    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        if (null == intent) {
            return super.onStartCommand(intent, flags, startId)
        }

        WeChatMotionMonitor.startWechat(this)

        return super.onStartCommand(intent, flags, startId)
    }


    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        //这个方法是我们用的最多的方法，我们会在这个方法里写大量的逻辑操作。
        //通过对event的判断执行不同的操作
        //当窗口发生的事件是我们配置监听的事件时,会回调此方法.会被调用多次
        val eventType = event?.eventType
        val packageName = event?.packageName?.toString()
        val className = event?.className?.toString()

        Log.e("AutoShareWXService", "etype=$eventType,pName=$packageName , className=$className")

        when (eventType) {
            AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED, AccessibilityEvent.TYPE_VIEW_SCROLLED, AccessibilityEvent.TYPE_VIEW_CLICKED -> {
                WeChatMotionMonitor.policy(rootInActiveWindow, packageName, className, this)
            }
        }

    }

    override fun onInterrupt() {

    }
}